package com.abl.service.position.impl;

import com.abl.common.mybatis.dao.Dao;
import com.abl.common.mybatis.model.Dnt;
import com.abl.common.mybatis.strengthen.Wrapper;
import com.abl.domain.model.AblError;
import com.abl.domain.model.Param;
import com.abl.common.util.StringKit;
import com.abl.service.position.PositionMapService;
import com.abl.service.system.SysService;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 商场定位系统服务实现类
 */
@Slf4j
@Service
public class PositionMapServiceImpl implements PositionMapService {
    @Autowired
    private Dao dao;

    @Autowired
    private SysService sysService;

    @Override
    public List<Dnt> list(Param<JSONObject> param) {
        //全部区域
        Wrapper<Object> qw = new Wrapper<>();
        qw.eq("d_s", "0");
        qw.eq("floor", param.getString("floor"));
        qw.orderByAsc("graph_sort");
        List<Dnt> p_area_map = dao.list("p_area_map", qw);
        return p_area_map;
    }

    // 匹配输入内容，按区域名称搜索
    @Override
    public List<Dnt> searchInput(Param<JSONObject> param) {
        String name = param.getOrDefault("name", "") + "";

        if (StringKit.isNotBlank(name)) {
            Wrapper<Object> qw = new Wrapper<>();
            qw.select("name");
            qw.eq("d_s", 0);
            qw.like("name", name);
            qw.groupBy("name");
            qw.last(" limit 20 ");
            return dao.list("p_area_map", qw);
        }

        return new ArrayList<>();
    }

    @Override
    public boolean add(Param<JSONObject> param) {
        String name = param.getString("name");
        String floor = param.getString("floor");
        String config = param.getString("config");
        JSONArray xy = param.getJSONArray("xy");

        //校验名称重复
        Wrapper<Object> qw = new Wrapper<>();
        qw.select("name");
        qw.eq("d_s", 0);
        qw.eq("name", name);
        qw.eq("floor", floor);
        long count = dao.count("p_area_map", qw);
        if (count > 0) throw new AblError("操作失败：【" + floor + "楼】 已存在名为 【" + name + "】 的区域");
        //插入
        Dnt insert = Dnt.createInsert();
        insert.set("name", name);
        insert.set("floor", floor);
        insert.set("config", config);
        insert.set("area_xy", xy.toString());
        if (StringKit.isBlank(config)) {
            insert.set("config", sysService.dictValue("map_default_config", "{}"));
        }
        dao.insert("p_area_map", insert);
        return true;
    }

    @Override
    public boolean edit(Param<JSONObject> param) {
        String id = param.getString("id");
        String name = param.getString("name");
        String floor = param.getString("floor");
        String code = param.getString("code");
        String graph_sort = param.getString("graph_sort", "100");
        JSONObject config = param.getJSONObject("config");
        JSONArray xy = param.getJSONArray("xy");


        //校验名称重复
        Wrapper<Object> qw = new Wrapper<>();
        qw.select("name");
        qw.eq("d_s", 0);
        qw.eq("name", name);
        qw.eq("floor", floor);
        qw.ne("id", id);
        long count = dao.count("p_area_map", qw);
        if (count > 0) throw new AblError("操作失败：【" + floor + "楼】 已存在名为 【" + name + "】 的区域");

        //插入
        Dnt update = Dnt.createUpdate();
        update.set("id", id);
        update.set("name", name);
        update.set("code", code);
        update.set("floor", floor);
        update.set("graph_sort", graph_sort);
        update.set("config", config.toString());
        update.set("area_xy", xy.toString());
        dao.updateById("p_area_map", update);
        return true;
    }

    @Override
    public long del(Param<JSONObject> param) {
        JSONArray ids = param.getJSONArray("ids");

        //条件
        Wrapper<Object> wrapper = new Wrapper<>();
        wrapper.in("id", ids);
        //数据
        Dnt update = Dnt.createUpdate();
        update.set("d_s", 1);

        return dao.update("p_area_map", update, wrapper);
    }


}
